package net.hotelOrderSystem.controller;

import net.hotelOrderSystem.domain.Hotel;
import net.hotelOrderSystem.domain.Order;
import net.hotelOrderSystem.domain.User;
import net.hotelOrderSystem.service.HotelService;
import net.hotelOrderSystem.service.OrderService;
import net.hotelOrderSystem.service.RoomService;
import net.hotelOrderSystem.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static net.hotelOrderSystem.controller.UserController.curAccount;

@Controller
@RequestMapping("/HTML/*")
public class AdminController  {

    @Autowired
    private UserService userService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private RoomService roomService;
    @Autowired
    private HotelService hotelService;
    //管理员查找用户列表
    @RequestMapping("/searchUser")
    public String searchUser(Model model){
        List<User> users =userService.findUserByPremission( userService.findUserByAccount(curAccount).getPremission());
        model.addAttribute("users",users);
        return "/HTML/adminShowUserInfoList";
    }
    //管理员查看用户详细信息
    @RequestMapping("adminUpdateUserInfo")
    public String toString(Model model,@RequestParam("account")String account) {
        User user = userService.findUserByAccount(account);
        model.addAttribute("user", user);
        model.addAttribute("orders",orderService.findOrderByAccount(account));
        model.addAttribute("adminPremission",userService.findUserByAccount(curAccount).getPremission());
        return "/HTML/adminUpdateUserInfo";
    }
    //管理员删除用户
    @RequestMapping("/delectUser")
    public String delectUser(Model model,@RequestParam("account")String account){
        System.out.println(account+"啊");
        userService.deleteUserByAccount(account);
        model.addAttribute("users",userService.findUserByPremission( userService.findUserByAccount(curAccount).getPremission()));
        return "/HTML/adminShowUserInfoList";
    }
    //管理员修改用户信息
    @RequestMapping("adminUpdateUserInfoTo")
    public  String updateUserInfoByAdmin(Model model, @RequestParam("user.account") String account,@RequestParam("user.age")Short age,
                                         @RequestParam("user.name")String name, @RequestParam("user.phone")String phone,
                                         @RequestParam("user.mail")String mail, @RequestParam("user.gender")String gender,
                                         @RequestParam("user.role")String role, @RequestParam("user.premission")String premission)
    {
        System.out.println("!123!!"+account);
        if(role==""){role=userService.findUserByAccount(account).getRole();}
        if(premission==""){premission=userService.findUserByAccount(account).getPremission();}
        if(age==null){age=userService.findUserByAccount(account).getAge();}
        if (name==""){name=userService.findUserByAccount(account).getName();}
        if (phone==""){phone=userService.findUserByAccount(account).getPhone();}
        if (mail==""){mail=userService.findUserByAccount(account).getMail();}
        if (gender==""){gender=userService.findUserByAccount(account).getGender();}
        User user =new User(account,age,name,phone,mail,gender,userService.findUserByAccount(account).getPassword(),role,premission);
        userService.updateUserByAccount(account,user);
        model.addAttribute("user", user);
        model.addAttribute("orders",orderService.findOrderByAccount(account));
        return  "/HTML/adminUpdateUserInfo";
    }
    //管理员查看订单列表
    @RequestMapping("adminShowOrderList")
    public String showAllOrderList(Model model){
        List<Order> orders = orderService.findOrder();
        model.addAttribute("orders",orders);
        return "/HTML/adminShowUserOrderList";
    }
    //管理员从列表删除订单
    @RequestMapping("delectFromOrderList")
    public  String delectFromOrderList(Model model,@RequestParam("orderId") String orderId){
        orderService.deleteOrderByID(orderId);
        model.addAttribute("orders",orderService.findOrder());
        return "/HTML/adminShowUserOrderList";
    }
    //管理员修改订单列表
    @RequestMapping("updateAllOrderList")
    public  String updateAllOrderList(Model model,@RequestParam("orders")List<Order> orders){
        for(int i=0;i<orders.size();i++)
            orderService.updateOrderByID(orders.get(i).getOrderId(),orders.get(i));
        model.addAttribute("allOrderList",orderService.findOrder());
        return "/HTML/allOrderList";
    }
    //管理员查看订单详情
    @RequestMapping("showOrderForAdmin")
    public String showOrderForAdmin(Model model,@RequestParam("orderId")String orderId){
        model.addAttribute("order",orderService.findOrderByID(orderId));
        return "/HTML/adminOrderHome";
    }
    //管理员修改订单详情
    @RequestMapping("updateOrderByAdmin")
    public String updateOrderbyAdmin(Model model,@RequestParam("orderId")String orderId,@RequestParam("account")String account,
                                     @RequestParam("roomType")String roomType,
                                     @RequestParam("inNumber")Short inNumber,  @RequestParam("inTime")String inTime,
                                     @RequestParam("outTime")String outTime, @RequestParam("roomNumber")String roomNumber,
                                     @RequestParam("paymentStatus") Integer paymentStatus,@RequestParam("orderStatus")Integer orderStatus) throws ParseException {
        System.out.println("?@"+orderId);
        String hotelName = orderService.findOrderByID(orderId).getHotelName();
        roomNumber=orderService.findOrderByID(orderId).getRoomNumber();
        int price = roomService.findRoomByRoomNumber(roomNumber).getPrice();

        if(inTime=="")inTime=orderService.findOrderByID(orderId).getInTime().toString();
        if(outTime=="")inTime=orderService.findOrderByID(orderId).getOutTime().toString();

        SimpleDateFormat inString=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date inDate=inString.parse(inTime);
        SimpleDateFormat outString=new SimpleDateFormat("yyyy-MM-dd");//小写的mm表示的是分钟
        Date outDate=outString.parse(outTime);
        int dayNum;
        System.out.println(inDate.getTime()+"!!"+outDate.getTime());
        dayNum = (int)((outDate.getTime()-inDate.getTime())/(1000*60*60*24));
        System.out.println(dayNum+"！"+price);
        price = price*(dayNum+1);
        if(roomType=="")roomType=orderService.findOrderByID(orderId).getRoomType();
        if(inNumber==null)inNumber=orderService.findOrderByID(orderId).getInNumber();

        Order order = new  Order(orderId,  account,  hotelName,  roomType,  inNumber,  inDate,  outDate,  orderStatus,  paymentStatus,  roomNumber,price);
        orderService.updateOrderByID(orderId,order);
        model.addAttribute("order",orderService.findOrderByID(orderId));
        return "/HTML/adminOrderHome";
    }
    //管理员管理酒店
    @PostMapping("adminMangeHotel")
    public String adminManageHotel(Model model){
        model.addAttribute("hotels",hotelService.findHotel());
        return "/HTML/adminShowHotelList";
    }
    //管理员查看酒店详情
    @RequestMapping("showHotelForAdmin")
    public String showHotelForAdmin(Model model,@RequestParam("hotelId")String hotelId){
        model.addAttribute("hotel",hotelService.findHotelByID(hotelId));
        return "/HTML/adminHotelHome";
    }
    //管理员修改酒店列表-删除
    @PostMapping("adminDeleteHotel")
    public String adminDeleteHotel(Model model, @RequestParam("hotelId")String hotelId){
        orderService.findOrderByHotelId(hotelId);
        int flag=0;
        List<Order> orders = orderService.findOrderByHotelId(hotelId);

        if( orders==null){
            System.out.println("0");
            hotelService.deleteHotelByID(hotelId);
            model.addAttribute("hotels",hotelService.findHotel());
            return "/HTML/adminShowHotelList";
        }
        else{
            flag=1;
            System.out.println("1");
            model.addAttribute("hotels",hotelService.findHotel());
            return "/HTML/adminShowHotelList";
        }
    }
    //+++管理员修改酒店列表-增加和修改
    @PostMapping("adminAddHotel")
    public String adminAddHotel (Model model,@RequestParam("addHotel") String hotelId,
                                 @RequestParam("hotelName")String hotelName,@RequestParam("hotelTranslatedName")String hotelTranslatedName,
                                 @RequestParam("chainId") String chainId,@RequestParam("chainName") String chainName,
                                 @RequestParam("brandId") String brandId,@RequestParam("brandName") String brandName,
                                 @RequestParam("hotelFormerlyName") String hotelFormerlyName,@RequestParam("addressline1") String addressline1,
                                 @RequestParam("addressline2") String addressline2,@RequestParam("zipcode") String zipcode,
                                 @RequestParam("city") String city,@RequestParam("state") String state,
                                 @RequestParam("country") String country,@RequestParam("countryisocode") String countryisocode,
                                 @RequestParam("starRating") Double starRating,@RequestParam("longitude") String longitude,
                                 @RequestParam("latitude") String latitude,@RequestParam("url") String url,
                                 @RequestParam("checkin") String checkin,@RequestParam("checkout") String checkout,
                                 @RequestParam("numberrooms") String numberrooms,@RequestParam("numberfloors") String numberfloors,
                                 @RequestParam("yearopened") String yearopened,@RequestParam("yearrenovated") String yearrenovated,
                                 @RequestParam("photo1") String photo1,@RequestParam("photo2") String photo2,
                                 @RequestParam("photo3") String photo3,@RequestParam("photo4") String photo4,
                                 @RequestParam("photo5") String photo5,@RequestParam("overview") String overview,
                                 @RequestParam("ratesFrom") String ratesFrom,@RequestParam("continentId") String continentId,
                                 @RequestParam("continentName") String continentName,@RequestParam("cityId") String cityId,
                                 @RequestParam("countryId") String countryId,@RequestParam("numberOfReviews") String numberOfReviews,
                                 @RequestParam("ratingAverage") Double ratingAverage,@RequestParam("ratesCurrency") String ratesCurrency,
                                 @RequestParam("priceLow") Integer priceLow,@RequestParam("priceTop") Integer priceTop){
        Hotel hotel = new Hotel(hotelId,  chainId,  chainName,  brandId,  brandName,  hotelName,  hotelFormerlyName,
                hotelTranslatedName,  addressline1,  addressline2,  zipcode,  city,  state,  country,  countryisocode,
                starRating,  longitude,  latitude,  url,  checkin,  checkout,  numberrooms,  numberfloors,  yearopened,
                yearrenovated,  photo1,  photo2,  photo3,  photo4,  photo5,  overview,  ratesFrom,  continentId,  continentName,
                cityId,  countryId,  numberOfReviews,  ratingAverage,  ratesCurrency,  priceLow,  priceTop) ;
        hotelService.saveHotel(hotel);
        model.addAttribute("hotel",hotel);
        return "/HTML/adminHotelHome";
    }
}
